package com.example.opengldemo.jbulletJava;
import android.view.View;

import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.CompoundShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.Transform;
import com.example.opengldemo.util.MatrixState;

public class CubeCylinder
{
	Cylinder cl;
	TexCube tc;
	float halfSize;
	RigidBody body;
	View mv;
	
	public CubeCylinder(View mv,float halfSize,CollisionShape[] csa,
			DiscreteDynamicsWorld dynamicsWorld,float mass,float cx,float cy,float cz,int[] mProgram)
	{	
		CompoundShape comShape=new CompoundShape();
		Transform localTransform = new Transform();
		localTransform.setIdentity();
		localTransform.origin.set(new Vector3f(0, 0, 0));
		comShape.addChildShape(localTransform, csa[0]);
		comShape.addChildShape(localTransform, csa[1]);
		localTransform = new Transform();
		localTransform.basis.rotX((float)Math.toRadians(90));
		comShape.addChildShape(localTransform, csa[2]);
		boolean isDynamic = (mass != 0f);
		Vector3f localInertia = new Vector3f(0, 0, 0);
		if (isDynamic)
		{
			comShape.calculateLocalInertia(mass, localInertia);
		}
		Transform startTransform = new Transform();
		startTransform.setIdentity();
		startTransform.origin.set(new Vector3f(cx, cy, cz));
		DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
		RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo
		(
				mass, myMotionState, comShape, localInertia
		);
		body = new RigidBody(rbInfo);
		body.setRestitution(0.6f);
		body.setFriction(0.8f);
		dynamicsWorld.addRigidBody(body);
		tc=new TexCube(halfSize,mProgram[0]);
		cl=new Cylinder(halfSize/2,halfSize*3.6f,16,mProgram[0],mv);
		this.halfSize=halfSize;
		this.mv=mv;
	}
	public void drawSelf(int[] texIda,int[] texIdb)
	{
		int texId1=texIda[0];
		int texId2=texIdb[1];
		if(!body.isActive())
		{
			texId1=texIda[1];
			texId2=texIdb[0];
		}
		MatrixState.pushMatrix();
		Transform trans=body.getMotionState().getWorldTransform(new Transform());
		MatrixState.translate(trans.origin.x,trans.origin.y, trans.origin.z);
		Quat4f ro=trans.getRotation(new Quat4f());
		if(ro.x!=0||ro.y!=0||ro.z!=0)
		{
			if(!Float.isNaN(ro.x)&&!Float.isNaN(ro.y)&&!Float.isNaN(ro.z)
					&&!Float.isInfinite(ro.x)&&!Float.isInfinite(ro.y)&&!Float.isInfinite(ro.z))
			{
				float[] fa=SYSUtil.fromSYStoAXYZ(ro);
				MatrixState.rotate(fa[0], fa[1], fa[2], fa[3]);
			}
		}
		tc.drawSelf( new int[]{texId1});
		MatrixState.pushMatrix();
		MatrixState.rotate(90,0, 0, 1);
		MatrixState.translate(0, -halfSize*1.8f, 0);
		cl.drawSelf(texId2,  texId2,  texId2);
		MatrixState.popMatrix();
		MatrixState.pushMatrix();
		MatrixState.translate(0, -halfSize*1.8f, 0);
		cl.drawSelf(texId2,  texId2,  texId2);
		MatrixState.popMatrix();
		MatrixState.popMatrix();
	}
}
